{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e900319e-f9f8-4fd2-8b05-7b0f2178dd15",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\programData\\conda\\env\\ChatGLM2-6B\\lib\\site-packages\\numpy\\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:\n",
      "F:\\programData\\conda\\env\\ChatGLM2-6B\\lib\\site-packages\\numpy\\.libs\\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll\n",
      "F:\\programData\\conda\\env\\ChatGLM2-6B\\lib\\site-packages\\numpy\\.libs\\libopenblas64__v0.3.23-246-g3d31191b-gcc_10_3_0.dll\n",
      "  warnings.warn(\"loaded more than 1 DLL from .libs:\"\n"
     ]
    }
   ],
   "source": [
    "#6.4.1. 多输入通道\n",
    "import torch\n",
    "from d2l import torch as d2l\n",
    "\n",
    "def corr2d_multi_in(X,K):\n",
    "    return sum(d2l.corr2d(x,k) for x,k in zip(X,K))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4193d5b7-8598-409b-80b9-5f3578b486a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 56.,  72.],\n",
       "        [104., 120.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n",
    "               [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])\n",
    "K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])\n",
    "\n",
    "corr2d_multi_in(X,K)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8af90fc2-22d6-4747-9b80-44bb657a0d9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 3, 3])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "073e7a41-ecc0-4f4f-b547-446d96aaf9bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2, 2])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c1a92d94-44c1-4338-8dff-b5f27838bb6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 2, 2, 2])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def corr2d_multi_in_out(X,K):\n",
    "    return torch.stack([corr2d_multi_in(X,k) for k in K],0)\n",
    "\n",
    "K = torch.stack((K,K+1,K+2),0)\n",
    "K.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a9639f9-f28a-4535-bab8-9af3b363ab06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 56.,  72.],\n",
       "         [104., 120.]],\n",
       "\n",
       "        [[ 76., 100.],\n",
       "         [148., 172.]],\n",
       "\n",
       "        [[ 96., 128.],\n",
       "         [192., 224.]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corr2d_multi_in_out(X,K)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "261e4305-70fc-4953-bd54-edd0162660dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#6.4.3.  1X1卷积层\n",
    "def corred_multi_in_out_1x1(X,K):\n",
    "    c_i,h,w = X.shape\n",
    "    c_o = K.shape[0]\n",
    "    X = X.reshape((c_i,h*w))\n",
    "    K = K.reshape((c_o,c_i))\n",
    "    Y = torch.matmul(K,X)\n",
    "    return  Y.reshape((c_o,h,w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1eade680-a1a1-4926-a7bf-57ab7b6a9466",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[-0.3586, -1.5871,  0.1131],\n",
       "          [-2.8219, -0.1497, -1.4170],\n",
       "          [ 1.9417, -6.4065, -4.9319]],\n",
       " \n",
       "         [[ 0.5200, -0.4373, -0.0845],\n",
       "          [-0.3868,  0.8042, -1.2372],\n",
       "          [ 0.4240,  1.0396,  0.6554]]]),\n",
       " torch.Size([2, 3, 3]),\n",
       " tensor([[[-0.3586, -1.5871,  0.1131],\n",
       "          [-2.8219, -0.1497, -1.4170],\n",
       "          [ 1.9417, -6.4065, -4.9319]],\n",
       " \n",
       "         [[ 0.5200, -0.4373, -0.0845],\n",
       "          [-0.3868,  0.8042, -1.2372],\n",
       "          [ 0.4240,  1.0396,  0.6554]]]),\n",
       " torch.Size([2, 3, 3]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.normal(0,1,(3,3,3))\n",
    "K = torch.normal(0,1,(2,3,1,1))\n",
    "\n",
    "Y1 = corred_multi_in_out_1x1(X,K)\n",
    "Y2 = corr2d_multi_in_out(X,K)\n",
    "Y1,Y1.shape,Y2,Y2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c5ba0de-4a23-4d18-9007-257cadeee07f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
